import {
  PersonModel
} from '../../models/personal.js'
let personModel = new PersonModel()
import {
  LoginModel
} from '../../models/login.js'
let loginModel = new LoginModel()
import { config } from '../../config.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      FName: '',
      FPhoto: '',
      FWorkTime: '',
      FWorkplace: '',
      FEmpStatus: '',
      FRemark: '',
      FMobile: '',
      FId: ''
    },
    photo: '',
    name: '',
    genders: [{
      name: '男',
      value: '1'
    }, {
      name: '女',
      value: '2'
    }],
    genderIndex: 0,
    status: [{
      name: '在岗求职',
      value: '1'
    }, {
      name: '正在上班',
      value: '2'
    }, {
      name: '待业求职',
      value: '3'
    }],
    statusIndex: 0,
    workDate: '',
    workPlace: '',
    mobile: '',
    otherDes: '',
  },

  genderSelector(e) {
    this.setData({
      genderIndex: e.detail.value
    })
  },

  dateSelector(e) {
    this.setData({
      workDate: e.detail.value
    })
  },

  statusSelector(e) {
    this.setData({
      statusIndex: e.detail.value
    })
  },

  pickPhoto() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.compressImage({
          src: res.tempFilePaths[0],
          quality: 70
        })
        that.setData({
          photo: res.tempFilePaths[0]
        })
      }
    })
  },

  nameInput(e) {
    this.setData({
      name: e.detail.value
    })
  },

  workPlaceInput(e) {
    this.setData({
      workPlace: e.detail.value
    })
  },

  otherDesInput(e) {
    this.setData({
      otherDes: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '编辑个人信息'
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3B80FE',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    this.setData({
      workDate: year + '-' + month + '-' + day,
      mobile: options.mobile
    })
    
  },

  requestPersonData() {
    personModel.getPersonalData()
      .then(res => {
        this.setData({
          info: res.data.data.records
        })
        this.submitPic()
      })
  },

  checkData() {
    if (!this.data.name) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写姓名'
      })
      return
    }
    if (!this.data.workPlace) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写工作地点'
      })
      return
    }
    wx.showLoading()
    this.register()
  },

  submitPic() {
    var that = this
    wx.uploadFile({
      url: `${config.api_base_url}api/Ecp.Picture.upload.jup`,
      filePath: that.data.photo,
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data",
        'Cookie': 'MEETREND_CRM_TOKEN=' + wx.getStorageSync('token')
      },
      formData: {
        'user': 'test'
      },
      success: function (res) {
        that.setData({
          'info.FPhoto': JSON.parse(res.data).data.pictureId
        })
        that.saveDataWithPic()
      },
      fail: function (e) {
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
      }
    })
  },

  saveDataWithPic() {
    personModel.savePersonalData({
      args: {
        data: [this.data.info]
      }
    }).then(res => {
      wx.hideLoading()
      wx.navigateTo({
        url: '/pages/workExperience/workExperience',
      })
    })
  },

  register() {
    this.setData({
      'info.FName': this.data.name,
      'info.FGender': this.data.genders[this.data.genderIndex].value,
      'info.FWorkTime': this.data.workDate,
      'info.FEmpStatus': this.data.status[this.data.statusIndex].value,
      'info.FWorkplace': this.data.workPlace,
      'info.FRemark': this.data.otherDes,
      'info.FMobile': this.data.mobile
    })
    loginModel.registerUser({
      args: {
        FMobile: this.data.mobile,
        data: this.data.info,
        userType: 'jobwant'
      }
    }).then(res => {
      wx.setStorageSync('token', res.data.data.token)
      if(this.data.photo.length > 0) {
        this.requestPersonData()
      } else {
        wx.hideLoading()
        wx.navigateTo({
          url: '/pages/workExperience/workExperience',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})